﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;

using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid;
using DevExpress.Utils;

using DevExpress.XtraEditors.Repository;

using MM.Entities;
using MM.Business; 

namespace MM
{
    public partial class frmBaoCaoNXT_Chitiet : Form
    {
        string _matau = "";
        string _nguoitao = "";
        string _sConn = "";
        string _plant = "";
        List<TON_KHO> lst = new List<TON_KHO>();
        TON_KHO objTK; 

        public frmBaoCaoNXT_Chitiet(string sConn,TON_KHO obj)
        {
            InitializeComponent();
            _sConn = sConn;

            _matau = frmDangNhap.objTau._tenTau2KiTu;
            _plant = frmDangNhap.objTau._plantReferenceSAP;

            objTK = new TON_KHO();
            objTK = obj;

            this.txtKho.Text = obj.TenKho;
            this.txtTenTrangThai.Text = obj.TenTrangThai;
            this.txtTenVTPT.Text = obj.TenVTPT;
            this.txtFromDate.Text = obj.FromDate.Day.ToString()+"/"+obj.FromDate.Month.ToString()+"/"+ obj.FromDate.Year.ToString();
            this.txtToDate.Text = obj.ToDate.Day.ToString() + "/" + obj.ToDate.Month.ToString() + "/" + obj.ToDate.Year.ToString();
            this.txtDauKy.Text = obj.DauKy.ToString();

            

            this.InitGrid(obj);

        }

        private void InitGrid(TON_KHO obj)
        {
            lst.Clear();
            clsTON_KHO tkCtrl = new clsTON_KHO(_sConn);
            
            lst = tkCtrl.usp_XemTON_KHO_Chitiet(obj);

            this.gridControl1.DataSource = lst;
            this.gridControl1.RefreshDataSource();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (lst.Count > 0)
                {
                    string filepath = "";
                    FolderBrowserDialog dg = new FolderBrowserDialog();
                    dg.ShowDialog();
                    if (dg.SelectedPath != "")
                    {
                        clsTON_KHO tkCtrl = new clsTON_KHO(_sConn);
                        // xuat file Csv
                        string y = DateTime.Today.Year.ToString();
                        string m = DateTime.Today.Month.ToString();
                        string d = DateTime.Today.Day.ToString();
                        m = Convert.ToString("0" + m).Substring(m.Length - 1, 2);
                        d = Convert.ToString("0" + d).Substring(d.Length - 1, 2);
                        filepath = dg.SelectedPath + "\\" + "ID" + _matau + "_" + y + m + d + ".csv";
                        tkCtrl.CsvExportTON_KHO_Chitiet(objTK, lst, filepath);
                        MessageBox.Show("XUẤT FILE THÀNH CÔNG!" + "\n" + "GỬI EMAIL ĐÍNH KÈM FILE TẠI: " + filepath + " VỀ ĐỊA CHỈ: vmm@vitranschart.com.vn");
                    }
                }
            }
            catch { }
        }

        private void frmBaoCaoNXT_Chitiet_Load(object sender, EventArgs e)
        {
            Common.SetColor(this);
        }
    }
}